package com.my.yc;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "MathServlet", value = "/MathServlet")
public class MathServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        final String a = request.getParameter("a");
        final String b = request.getParameter("b");

        response.setContentType("text/html;charset=utf-8");
        int ia,ib;
        try {
            ia = Integer.parseInt(a);
        } catch (NumberFormatException e) {
            response.getWriter().println("请输入正确的a参数值!");
            return;
        }

        try {
            ib = Integer.valueOf(b);
        } catch (NumberFormatException e) {
            response.getWriter().println("请输入正确的b参数值!");
            return;
        }
        final String op = request.getParameter("op");
        if (op.equalsIgnoreCase("+")){
            response.getWriter().println(ia + ib);
        }else if(op.equalsIgnoreCase("-")){
            response.getWriter().println(ia - ib);
        }else if(op.equalsIgnoreCase("*")){
            response.getWriter().println(ia * ib);
        }else if(op.equalsIgnoreCase("/")){
            if (ia == 0){
                response.getWriter().println("被除数不能为0!");
                return;
            }
            response.getWriter().println(ia / ib);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
